package com.geek.javase.ch13.work.singleton;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/*
 *@author wangpeng
 *2025/10/26 17:08
 *@description TODO
 */
public class SingletonTest {
    public static void main(String[] args) {
        try {
            Singleton originalInstance = Singleton.getInstance();
            System.out.println("原始实例: " + originalInstance);

            // 序列化
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("singleton.txt"));
            oos.writeObject(originalInstance);
            oos.close();

            // 反序列化
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("singleton.txt"));
            Singleton deserializedInstance = (Singleton) ois.readObject();
            ois.close();

            System.out.println("反序列化实例: " + deserializedInstance);
            System.out.println("是否为同一实例: " + (originalInstance == deserializedInstance));


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
